package com.yourwork.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.yourwork.model.ServiceProject;
import com.yourwork.service.ServiceProjectService;

@Controller
@RequestMapping(value="/serviceProject")
/**
 * 服务管理表
 * */
public class ServiceProjectController {
	@Autowired
	@Qualifier(value="serviceItemService")
	private ServiceProjectService serviceItemService;
	/**
	 * 查询服务类表
	 * @param session 储存值
	 * @param m 与页面进行数据传递
	 * */
	@RequestMapping(value="/select",method={RequestMethod.GET})
	public String queryServiceItem(HttpSession session,Model m,
			@RequestParam(name="page",required=false,defaultValue="0") int page,
			@RequestParam(name="size",required=false,defaultValue="1000") int size,
			@RequestParam(name="pageFalse",required=false,defaultValue="0") int pageFalse,
			@RequestParam(name="status",required=false,defaultValue="true") Boolean status){
		try{
		List<ServiceProject> pageServiceItemTrue = serviceItemService.queryServiceItemAllF(page,size,true);//查询出全部的服务信息
		List<ServiceProject> pageServiceItemFalse = serviceItemService.queryServiceItemAllF(pageFalse,size,false);//查询出全部的服务信息
		int maxPageTrue = serviceItemService.queryMaxPage(page,size,status);
		int maxPageFalse = serviceItemService.queryMaxPage(page,size,status);
		m.addAttribute("maxPageTrue", maxPageTrue);//最大页数
		m.addAttribute("maxPageFalse", maxPageFalse);//最大页数
		m.addAttribute("page", page);//attribute中存入page
		m.addAttribute("pageFalse", pageFalse);//attribute中存入page
		m.addAttribute("size", size);//attribute中存入size
		m.addAttribute("pageServiceItemTrue", pageServiceItemTrue);//将服务信息的List对象存入attribute
		m.addAttribute("pageServiceItemFalse", pageServiceItemFalse);//将服务信息的List对象存入attribute
		m.addAttribute("remind", session.getAttribute("remind"));//将提示信息存入attribute
		session.setAttribute("remind", "");//将提示信息清空
		m.addAttribute("managerNumber", session.getAttribute("managerNumber"));
		m.addAttribute("employeeType", serviceItemService.queryEmployeeType());
		return "/work/serviceItem";}
		catch (Exception e) {
			// TODO: handle exception
			return "/work/serviceItem";
		}
	}
	/**
	 * 添加服务类型
	 * @param serviceItem 与页面的表单绑定
	 * @param session 储存值
	 * */
	@RequestMapping(value="/addserviceProject",method={RequestMethod.POST})
	public String insertServiceItem(@ModelAttribute(name="serviceItem") ServiceProject serviceItem,
		@RequestParam(name="type") int[] typrId,HttpSession session){
		System.out.println("进入");
		String remind = serviceItemService.insertServiceItem(serviceItem,typrId);
		session.setAttribute("remind", remind);
		return	"redirect:/serviceProject/select";
	}
	/**
	 * 删除服务类型
	 * session 储存
	 * */
	@RequestMapping(value="/deleteServiceItem")
	public String updateStatus(@ModelAttribute("serviceItem") ServiceProject serviceItem,
			HttpSession session){
		String remind = serviceItemService.updateStatus(serviceItem);
		session.setAttribute("remind", remind);
		return "redirect:/serviceProject/select";
		}
	}
